import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';

import '../../res/colors.dart';

/// 标题栏
class TitleBar extends StatelessWidget implements PreferredSizeWidget {
  const TitleBar({
    Key? key,
    this.title = '',
    this.backgroundColor = Colors.white,
    this.mainColor = Colors.black,
    this.needDivider = false,
    this.needBack = true,
    this.backIcon,
    this.onBackPressed,
    this.actions,
    this.centerTitle,
    this.rightText,
    this.rightIcon,
    this.titleWidget,
    this.onRightPressed,
  }) : super(key: key);

  final String title;
  final Color backgroundColor;
  final bool needDivider;
  final bool needBack;
  final Color? mainColor;
  final Widget? backIcon;
  final Widget? titleWidget;
  final VoidCallback? onBackPressed;
  final List<Widget>? actions;
  final bool? centerTitle;
  final String? rightText;
  final IconData? rightIcon;
  final VoidCallback? onRightPressed;

  @override
  Size get preferredSize => const Size.fromHeight(56);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: titleWidget ??
          Text(
            title,
            style: TextStyle(fontSize: 17, color: mainColor),
          ),
      backgroundColor: backgroundColor,
      elevation: 0,
      scrolledUnderElevation: 0,
      leading: backIcon ??
          (needBack
              ? IconButton(
                  onPressed: onBackPressed ??
                      () {
                        BoostNavigator.instance.pop();
                      },
                  icon: Icon(Icons.arrow_back_ios,
                      size: 20,
                      color: mainColor ?? Theme.of(context).iconTheme.color))
              : null),
      centerTitle: centerTitle ?? true,
      actions: actions,
      //
      bottom: needDivider ? _DividerWidget() : null,
    );
  }
}

class _DividerWidget extends StatelessWidget implements PreferredSizeWidget {
  @override
  Size get preferredSize => const Size.fromHeight(1);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: YouthColors.color_EEEEEE,
      height: preferredSize.height,
    );
  }
}
